package cn.cq.admin.service.impl;

import cn.cq.admin.entity.vo.LoginLogVo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cq.admin.entity.model.TLoginLog;
import cn.cq.admin.service.TLoginLogService;
import cn.cq.admin.mapper.TLoginLogMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author  dicraft
* @description 针对表【t_login_log(用户登录日志表)】的数据库操作Service实现
*/
@Service
public class TLoginLogServiceImpl extends ServiceImpl<TLoginLogMapper, TLoginLog>
    implements TLoginLogService{

    @Override
    public SaResult doLoginLog() {
        Long userId = StpUtil.getLoginIdAsLong();
        QueryWrapper<TLoginLog> loginLogQw = new QueryWrapper<>();
        loginLogQw.eq("user_id", userId);
        loginLogQw.orderByDesc("create_time");
        loginLogQw.last("limit 5");
        List<TLoginLog> loginLogList = list(loginLogQw);
        List<LoginLogVo> loginLogVoList = loginLogList.stream()
                .map(loginLog -> convertToVo(loginLog)).collect(java.util.stream.Collectors.toList());
        return SaResult.data(loginLogVoList);
    }

    /**
     * @author jmz
     * @description 将实体转换为VO
     * @param loginLog
     * @return
     */
    private LoginLogVo convertToVo(TLoginLog loginLog) {
        LoginLogVo loginLogVo = new LoginLogVo();
        BeanUtils.copyProperties(loginLog, loginLogVo);
        return loginLogVo;
    }
}




